package cn.kgc.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author: 课工场
 * @since 2023/8/31
 * 通过日志打印调试信息
 * 日志级别
 * debug < info < warn < error
 * log.debug();
 * log.info();
 * log.warn();
 * log.error();
 */
@RestController
@RequestMapping("student")
@Slf4j
public class StudentController {

    /*
    * 引入ribbon组件实现服务调用的负载均衡
    * 1.DiscoveryClient discoveryClient
    * 2.LoadBalancerClient loadBalancerClient
    * */

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("m1")
    public String method1() {
        // 调用examController中的  localhost:8503/exam/m1    http请求
        // 创建restTemplate对象 发送http请求
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject("http://" + getHost() + "/exam/m1", String.class);
        // 打印日志信息   result = ?
        log.debug("debug result={}", result);
        return "studentService ok result =  " + result;
    }



    @GetMapping("m2")
    public String method2() {
        // 调用examController中的  localhost:8503/exam/m1    http请求
        // 创建restTemplate对象 发送http请求
        RestTemplate restTemplate = new RestTemplate();

        // url  http://localhost:8503/exam/m1
        // 通过ribbon获取可用节点 DiscoveryClient
        List<ServiceInstance> examservice = discoveryClient.getInstances("EXAMSERVICE");

        Random random = new Random();
        int i = random.nextInt(examservice.size());
        ServiceInstance serviceInstance = examservice.get(i);
        URI uri = serviceInstance.getUri();
        log.debug("uri = {}",uri);

        String result = restTemplate.getForObject(uri + "/exam/m1", String.class);

        return "studentService ok result =  " +result ;
    }


    @GetMapping("m3")
    public String method3() {

        RestTemplate restTemplate = new RestTemplate();
        // uri  默认采用轮循策略
        ServiceInstance examservice = loadBalancerClient.choose("EXAMSERVICE");
        URI uri = examservice.getUri();
        log.debug("uri = {}",uri);
        String result = restTemplate.getForObject(uri + "/exam/m1", String.class);
        return "studentService ok result =  "+result;
    }

    // 路径耦合 不利于代码的维护
    @GetMapping("m4")
    public String method4() {
        // 通过自动注入的restTemplate 实现请求的发送
        String result = restTemplate.getForObject("http://EXAMSERVICE/exam/m1", String.class);
        return "studentService ok result =  "+result;
    }

    /**
     *  返回随机节点
     *  1.节点的健康状态 无法控制
     *  2.节点的数量无法控制   注册中心  eureka consul nacos
     *  3.节点选择的策略单一  随机 权重 轮循 等  服务间调用的负载均衡机制
     */
    public String getHost() {
        // 创建服务节点集合
        List<String> hosts = Arrays.asList("localhost:8503", "localhost:8504", "localhost:8505");
        Random random = new Random();
        int index = random.nextInt(hosts.size());
        String host = hosts.get(index);
        return host;
    }

}
